package cn.cmcc.online.smsapi.sdk.db.entity;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.db.base.BaseManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;

public class PhoneSmsParseManager {
    public static final String TABLE_NAME = "tb_phone_bubble_cache";
    public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;
    public static String getCreateTableSql() {
        return "create table  if not exists " + TABLE_NAME + " ( "
                + " id INTEGER PRIMARY KEY,"
                + " phone TEXT UNIQUE,"
                + " minReceiveTime LONG default 0,"
                + " maxReceiveTime LONG default 0,"
                + " useBubbleViews TEXT,"
                + " useBubbleLogoName TEXT,"
                + " extend TEXT" + ")";
    }
    public static JSONObject findObjectByPhone(String phone){
        JSONObject jsonObject=null;
        XyCursor cursor=null;
        try {
            String [] cols = new String[] {
                    "id","phone","minReceiveTime","maxReceiveTime",
                    "useBubbleViews","useBubbleLogoName","extend" };
            cursor = DBManager.query(TABLE_NAME, cols,"phone = ?", new String[] {phone});
            jsonObject= BaseManager.loadSingleDataFromCursor(cols,cursor);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "findObjectByPhone: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
             XyCursor.closeCursor(cursor, true);
        }
        return jsonObject;
    }
    public static void reSetAllDataTime(String phone,long smsReceiveTime,String bubbleViewStr,String bubbleLogo,String extend){
        ContentValues values = new ContentValues();
        values.put("minReceiveTime", System.currentTimeMillis());
        values.put("maxReceiveTime", 0);
        try {
            DBManager.update(TABLE_NAME, values, null, null);
        }catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.e(Constant.TAG, "reSetAllDataTime: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
    }
    public static long saveOrUpdateObject(String phone,long smsReceiveTime,String bubbleViewStr,String bubbleLogo,String extend){
        long res = -1;
        try{
            JSONObject jsonObject=findObjectByPhone(phone);
            if(jsonObject == null){
                JSONArray bubbleViewArr = new JSONArray();
                if(!StringUtils.isNull(bubbleViewStr)){
                    bubbleViewArr.put(0, bubbleViewStr);
                }
                ContentValues values= null;
                String minTime = null;
                String maxTime = null;
                if(smsReceiveTime > 0){
                    minTime = maxTime = String.valueOf(smsReceiveTime);
                }else{
                    maxTime=String.valueOf(System.currentTimeMillis()+Integer.MAX_VALUE);
                    minTime="0";
                }
//                LogManager.d("duoqu_xiaoyuan", "minTime: "+minTime+" maxTime: "+maxTime+" smsReceiveTime: "+smsReceiveTime);
                values=BaseManager.getContentValues(null,true,"phone",phone,
                        "minReceiveTime",minTime,
                        "maxReceiveTime",maxTime,
                        "useBubbleViews",bubbleViewArr.toString(),
                        "useBubbleLogoName",bubbleLogo,
                        "extend",extend);
                if(values != null){
                    res = DBManager.insert(TABLE_NAME, values);
                }
            }else{
               ContentValues values= new ContentValues();
                if(smsReceiveTime > 0){
                    if(smsReceiveTime < Long.valueOf(jsonObject.getString("maxReceiveTime"))){
                        values.put("maxReceiveTime", smsReceiveTime);
//                        LogManager.d("duoqu_xiaoyuan", "maxReceiveTime  smsReceiveTime: "+smsReceiveTime);
                    }else if(smsReceiveTime > Long.valueOf(jsonObject.getString("minReceiveTime"))){
                        values.put("minReceiveTime", smsReceiveTime);
//                        LogManager.d("duoqu_xiaoyuan", "minReceiveTime  smsReceiveTime: "+smsReceiveTime);
                    }
                }
                if(!StringUtils.isNull(bubbleLogo)){
                    values.put("useBubbleLogoName", bubbleLogo);
                }
                if(!StringUtils.isNull(extend)){
                    values.put("extend", extend);
                }
                if(!StringUtils.isNull(bubbleViewStr)){
                    String useBubbleViews=jsonObject.getString("useBubbleViews");
                    JSONArray bubbleViewArr =null;
                    if(StringUtils.isNull(useBubbleViews)){
                        bubbleViewArr = new JSONArray();
                        bubbleViewArr.put(0, bubbleViewStr);
                    }else{
                        
                        
                        bubbleViewArr = new JSONArray(useBubbleViews);
                        int len = bubbleViewArr.length();
                        int i = 0;
                        for(;i < len;i++){
                            if(bubbleViewStr.equals(bubbleViewArr.getString(i))){
                                break;
                            }
                        }
                        if(i == len){
                            bubbleViewArr.put(len, bubbleViewStr);
                            values.put("useBubbleViews", bubbleViewArr.toString());
                        }
                    }
                }
                /* SDK-578 huangzhiqiang 20160804 start */
                if(values.size() > 0){
                    DBManager.update(TABLE_NAME, values, "phone=?", new String[]{phone});
                    res = Long.valueOf(jsonObject.getString("id"));
                }
                /* SDK-578 huangzhiqiang 20160804 end */
            }
        }catch(Throwable e){
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "saveOrUpdateObject: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
        return res;
    }
    public static void addInsertQueue(String phone,long smsReceiveTime,String bubbleViewStr,String bubbleLogo,String extend){
        saveOrUpdateObject(phone,smsReceiveTime,bubbleViewStr,bubbleLogo,extend);
    }
}
